package com.lovo.csc.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.PscRemote.IPscVendorRemote;
import com.lovo.commom.util.ConstantUtil;
import com.lovo.csc.bean.TenderVendorBean;
import com.lovo.csc.bean.UserBean;
import com.lovo.csc.bean.VendorBean;
import com.lovo.csc.mapper.ITenderVendorMapper;
import com.lovo.csc.mapper.IVendorMapper;
import com.lovo.csc.service.IVendorService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class VendorServiceImpl implements IVendorService {
    @Autowired
    IVendorMapper vendorMapper;
    @Autowired
    IPscVendorRemote pscVendorRemote;
    @Autowired
    ITenderVendorMapper tenderVendorMapper;

    @Override
    public Page<VendorBean> findByVendorStatus(Integer pageNO) {
        QueryWrapper<VendorBean> qw = new QueryWrapper<>();
        qw.eq("v_status", 0);
        return vendorMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public VendorBean findById(long vendorId) {
        return vendorMapper.selectById(vendorId);
    }

    @Override
    public void updateVendorStatus(long vendorId, Integer status, String str) {
        VendorBean vendorBean = vendorMapper.selectById(vendorId);
        vendorBean.setStatus(status);
        vendorBean.setCause(str);
        vendorMapper.updateById(vendorBean);
        //远程调用供应商接口
        pscVendorRemote.updateStatus(vendorBean.getName(), status, str);

    }

    @RabbitListener(queues = "queuePscHjh")
    public void getVendorInfo(String info) {
        VendorBean vendorBean = JSONObject.parseObject(info, VendorBean.class);
        QueryWrapper<VendorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", vendorBean.getName());
        VendorBean vendorBean1 = vendorMapper.selectOne(qw);
        if (vendorBean1 == null) {
            vendorBean.setStatus(0);
            vendorMapper.insert(vendorBean);
            ConstantUtil.WS_MSG.add("供应商管理");
        } else {
            vendorBean1.setFirm(vendorBean.getFirm());
            vendorBean1.setBank(vendorBean.getBank());
            vendorBean1.setAddress(vendorBean.getAddress());
            vendorBean1.setAptitudeImage(vendorBean.getAptitudeImage());
            vendorBean1.setIdImage(vendorBean.getIdImage());
            vendorBean1.setLeaderName(vendorBean.getLeaderName());
            vendorBean1.setLeaderPhone(vendorBean.getLeaderPhone());
            vendorBean1.setRanges(vendorBean.getRanges());
            vendorBean1.setAddress(vendorBean.getAddress());
            vendorBean1.setPwd(vendorBean.getPwd());
            vendorBean1.setStatus(0);
            vendorMapper.updateById(vendorBean1);
            ConstantUtil.WS_MSG.add("供应商管理");
        }
    }

    @Override
    public List<VendorBean> findAllSupper() {
        QueryWrapper<VendorBean> qw = new QueryWrapper<>();
        return vendorMapper.selectList(qw);
    }

    @Override
    public void updateTenderVendor(String vendorName, String tenderIdNum, String url) {
        QueryWrapper<VendorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", vendorName);
        VendorBean vendorBean = vendorMapper.selectOne(qw);
        QueryWrapper<TenderVendorBean> qwtv = new QueryWrapper<>();
        qwtv.eq("fk_vendorId", vendorBean.getVendorId());
        qwtv.eq("fk_tenderIdNum", tenderIdNum);
        TenderVendorBean tenderVendorBean = tenderVendorMapper.selectOne(qwtv);
        tenderVendorBean.setSrc(url);
        tenderVendorBean.setStatus(1);
        tenderVendorMapper.updateById(tenderVendorBean);
        ConstantUtil.WS_MSG.add("报价审核");
    }
}
